import process from 'process'
import {callFileCommand, callLogCommand, FILE_COMMAND_PATH} from '../src/cmd'
import path from 'path'
import * as stepPath from '../src/path'
import {afterEach} from 'mocha'
import {expect} from 'chai'
import * as cmdFile from '../src/cmd/file'
import * as cmdLog from '../src/cmd/log'
import sinon from 'sinon'

describe('addPath Test Case', function () {
    describe('add path by file command', function () {
        let callFileCommandStub: sinon.SinonStub
        let originalPath: string | undefined
        let originalFlowPath: string | undefined
        beforeEach(function () {
            callFileCommandStub = sinon.stub(cmdFile, 'callFileCommand')
            originalPath = process.env.PATH
            originalFlowPath = process.env.FLOW_PATH
        })
        it('add path by file command', () => {
            process.env.FLOW_PATH = '/flow/to/path'
            process.env.PATH = '/path1/hello:/path2/world'
            stepPath.addPath('/path/to/add')
            expect(callFileCommandStub.calledWith('PATH', '/path/to/add')).to.be
                .ok
            const newPath = process.env.PATH
            expect(newPath).to.equal('/path/to/add:/path1/hello:/path2/world')
        })

        it('add a empty path do nothing', () => {
            process.env.FLOW_PATH = '/flow/to/path'
            process.env.PATH = '/path1/hello:/path2/world'
            stepPath.addPath('')
            expect(callFileCommandStub.callCount).to.equal(0)
            const newPath = process.env.PATH
            expect(newPath).to.equal('/path1/hello:/path2/world')
        })

        afterEach(function () {
            process.env.PATH = originalPath
            process.env.FLOW_PATH = originalFlowPath
            callFileCommandStub.restore()
        })
    })

    describe('test add path by log', () => {
        let callLogCommandStub: sinon.SinonStub
        let originalPath: string | undefined
        let originalFlowPath: string | undefined
        beforeEach(function () {
            callLogCommandStub = sinon.stub(cmdLog, 'callLogCommand')
            originalPath = process.env.PATH
            originalFlowPath = process.env.FLOW_PATH
        })

        it('add path by command log', () => {
            process.env.FLOW_PATH = ''
            process.env.PATH = '/path1/hello:/path2/world'
            stepPath.addPath('/path/to/add')
            callLogCommandStub.calledWith('add-path', {}, '/path/to/add')
            const newPath = process.env.PATH
            expect(newPath).to.equal('/path/to/add:/path1/hello:/path2/world')
        })

        afterEach(function () {
            process.env.PATH = originalPath
            process.env.FLOW_PATH = originalFlowPath
            callLogCommandStub.restore()
        })
    })
})
